/*
  Copyright (C) 2011 Zhao Ming(zhao.ming29@gmail.com)

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "RecvBuf.h"
#include <memory.h>
int CRecvBuf::MaxBufLen = (1<<17);
CRecvBuf::CRecvBuf( int ol, int demodOL )
{
	buf = new char[MaxBufLen+ol];
	overLap = ol;
	writePos = 0;
	xcorrPos = 0;
	preRead = 0;
	demodLen = demodOL;
}

CRecvBuf::~CRecvBuf()
{
	delete buf;
}

int CRecvBuf::put( char *t, int k )
{
	int from,to,len;
	memcpy( buf+writePos, t, k );
	if( writePos<overLap )
	{
		from = 0;
		to = MaxBufLen+writePos;
		if( writePos+k>=overLap )
			len = overLap-writePos;
		else
			len = k;		
		memcpy(buf+to,t+from,len);	
	}
	if( writePos+k>MaxBufLen )
	{
		from = MaxBufLen - writePos;
		to = 0;
		len = writePos+k-MaxBufLen;
		memcpy(buf+to,t+from,len);	
	}
	writePos += k;
	if( writePos>=MaxBufLen )
		writePos -= MaxBufLen;
	int diff = writePos-xcorrPos;
	if( diff<0 )
		diff += MaxBufLen;
	if( diff > MaxBufLen-demodLen )
		return -1;
	else
		return 0;
}

int CRecvBuf::get( char **t, int k )
{
	if( xcorrPos>=MaxBufLen )
		xcorrPos-=MaxBufLen;
	int diff = writePos-xcorrPos;
	if( diff<0 )
		diff += MaxBufLen;
	if( diff<demodLen )
		return -1;
	preRead = -k;
	*t = buf+xcorrPos;
	xcorrPos -= preRead;
	return 0; 	
}

int CRecvBuf::rolOut( int *t, int offset, int rI, int rQ, int Mask[], int len )
{
	int pos = xcorrPos+preRead+offset;
	if( pos>=MaxBufLen )
		pos -= MaxBufLen;
	int i;
	char *pBuf = buf+pos;
	for( i=0;i<len;i++ )
	{
		t[i] = ((rI*pBuf[4*i]+rQ*pBuf[4*i+1])*Mask[i])>>8;	
	}	
	return 0;
}

int CRecvBuf::maskSum( int offset, int& I, int& Q, int Mask[], int len )
{
	int pos = xcorrPos+preRead+offset;
	if( pos>=MaxBufLen )
		pos -= MaxBufLen;
	int i;
	char *pBuf = buf+pos;
	I=0;
	Q=0;
	for( i=0;i<len;i++ )
	{
		I += pBuf[4*i]*Mask[i];
		Q += pBuf[4*i+1]*Mask[i];	
	}	
	return 0;
}

#ifdef RBUFTEST
#include "LRCTransmiter.h"
#include "macAddr.h"
#include "GHGS.h"
#include "RecvBuf.h"
#include "Wash.h"

#include<stdio.h>
#include<memory.h>

int main()
{
	int D[] = {64,   4,   8,   2,  16,   1,  32};
	int W[] = {-1,  -1,  -1,   1,   1,   1,   1};
	int frameLen = (128+2*640);
	char *tBuf = new char[frameLen*4];
	char *t = new char[frameLen/8];
	char *mask = new char[frameLen/8];
	int *iMask = new int[frameLen];
	CLRCTransmiter *aT = new CLRCTransmiter(1,1,2);
	GHGS *ghgsI = new GHGS(7,D,W);
	CWash *aWash = new CWash(9);
	CRecvBuf *rBuf = new CRecvBuf(frameLen*4,frameLen*4);
	MacAddr *addr = new MacAddr(1,1);
	addr->genCode( mask, 640*2 );
	int c;
	int *washBuf = new int[512];
	int i,j;
	for( i=0;i<2*640;i++ )
	{
		c = mask[i/8]>>(i%8);
		c &= 1;
		iMask[i]=1-2*c;
	}
	for( i=0;i<200;i++ )
	{
		int mes[2]={i,1023-i};
		aT->send(t,	mes, 2);
		memset(tBuf,0,frameLen*4);
		for( j=0;j<frameLen;j++ )
		{
			c = t[j/8]>>(j%8);
			c&=1;
			tBuf[4*j]=1-2*c;
		}
		if( rBuf->put(tBuf,frameLen*4)!=0 )
			printf("buffer overflow\n");
		char *xcorr;
		while( rBuf->get(&xcorr,1024)==0 )
		{
			int offset = 0;
			int I,T;
			for( offset=0;offset<1024;offset+=4 )
			{
				ghgsI->doit( (int)xcorr[offset],I,T );
				if( I==128 )
				{
					offset+=4;
					printf("find pilot at %d\n",offset);
					rBuf->maskSum( offset+512*4, I, T, iMask+512, 128 );
					printf("pilot1 I=%d Q=%d\n",I,T);
					rBuf->maskSum( offset+(640+512)*4, I, T, iMask+512+640, 128 );
					printf("pilot2 I=%d Q=%d\n",I,T);
					rBuf->rolOut(washBuf,offset,256,0,iMask,512);
					int res1=aWash->rDec(washBuf);		
					rBuf->rolOut(washBuf,offset+640*4,256,0,iMask+640,512);
					int res2=aWash->rDec(washBuf);
					printf("decode to %d:%d\n",res1,res2);
				}
			}
		}
	}
	delete tBuf;
	delete t;
	delete mask;
	delete iMask;
	delete aT;
	delete ghgsI;
	delete aWash;
	delete rBuf;
	delete addr;
	delete washBuf;
	return 0;
}
#endif